package com.mldong.modules.sys.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.mldong.auth.Perm;
import com.mldong.base.CommonPage;
import com.mldong.base.CommonResult;
import com.mldong.base.IdParam;
import com.mldong.base.IdsParam;
import com.mldong.context.constant.ConstantContextHolder;
import com.mldong.modules.sys.dto.FileInfoPageParam;
import com.mldong.modules.sys.dto.FileInfoParam;
import com.mldong.modules.sys.service.FileInfoService;
import com.mldong.modules.sys.vo.FileInfoVO;
import com.mldong.validation.Groups;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
* <p>
    * 文件信息 前端控制器
    * </p>
*
* @author mldong
* @since 2022-10-23
*/
@RestController
@Api(tags = "文件信息管理")
@RequiredArgsConstructor
public class FileInfoController {
    private final FileInfoService fileInfoService;
    private final FileStorageService fileStorageService;//注入实列

    /**
     * 添加文件信息
     * @param param
     * @return
     */
    @PostMapping("/sys/fileInfo/save")
    @ApiOperation(value = "添加文件信息")
    @Perm
    public CommonResult<?> save(@RequestBody @Validated({Groups.Save.class}) FileInfoParam param) {
        fileInfoService.save(param);
        return CommonResult.ok();
    }
    /**
     * 删除文件信息
     * @param param
     * @return
     */
    @PostMapping("/sys/fileInfo/remove")
    @ApiOperation(value = "删除文件信息")
    @Perm
    public CommonResult<?> remove(@RequestBody IdsParam param) {
        fileInfoService.removeBatchByIds(param.getIds());
        return CommonResult.ok();
    }
    /**
     * 修改文件信息
     * @param param
     * @return
     */
    @PostMapping("/sys/fileInfo/update")
    @ApiOperation(value = "修改文件信息")
    @Perm
    public CommonResult<?> update(@RequestBody @Validated({Groups.Update.class}) FileInfoParam param) {
        fileInfoService.update(param);
        return CommonResult.ok();
    }
    /**
     * 查询单个文件信息
     * @param param
     * @return
     */
    @PostMapping("/sys/fileInfo/detail")
    @ApiOperation(value = "查询单个文件信息")
    @Perm
    public CommonResult<FileInfoVO> detail(@RequestBody IdParam param) {
        FileInfoVO fileInfo = fileInfoService.findById(param.getId());
        return CommonResult.data(fileInfo);
    }
    /**
     *分页查询文件信息列表
     * @param param
     * @return
     */
    @PostMapping("/sys/fileInfo/page")
    @ApiOperation(value = "分页查询文件信息列表")
    @Perm
    public CommonResult<CommonPage<FileInfoVO>> page(@RequestBody FileInfoPageParam param) {
        return CommonResult.data(fileInfoService.page(param));
    }
    /**
     *上传文件
     * @return
     */
    @PostMapping("/sys/fileInfo/upload")
    @ApiOperation(value = "上传文件")
    @Perm
    public CommonResult<Dict> upload(MultipartFile file,
                                     @RequestParam(defaultValue = "default") String bizType,
                                     @RequestParam(defaultValue = "1") Integer persist
    ) {
        Dict dict = Dict.create();
        Long fileInfoId = IdWorker.getId();
        FileInfo fileInfo = fileStorageService.of(file).setPath(
                bizType+"/"+
                DateUtil.format(new Date(),"yyyyMM/"))
                .setObjectId(fileInfoId)
                .putAttr("bizType",bizType)
                .putAttr("persist",persist)
                .upload();
        String baseUrl = ConstantContextHolder.getImgBaseUrl(true);
        System.err.println(JSONUtil.toJsonPrettyStr(fileInfo));
        dict.put("fileInfoId", fileInfoId);
        String url = StrUtil.format("{}{}{}",
                fileInfo.getBasePath(),
                fileInfo.getPath(),
                fileInfo.getFilename());
        // 全地址
        dict.put("fullUrl",baseUrl + url);
        // 相对地址 ${basePath}/${bizType}/${yyyyMM}/${filename}
        dict.put("url", url);
        return CommonResult.data(dict);
    }
}
